package com.ruoyi.sales.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.sales.domain.ZcStoreInfo;
import com.ruoyi.sales.service.IZcStoreInfoService;
import com.ruoyi.system.service.ISysUserService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.sales.domain.ZcStoreEnter;
import com.ruoyi.sales.service.IZcStoreEnterService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 入库Controller
 * 
 * @author woo
 * @date 2024-04-11
 */
@RestController
@RequestMapping("/sales/storeEnter")
public class ZcStoreEnterController extends BaseController
{
    @Autowired
    private IZcStoreEnterService zcStoreEnterService;
    @Autowired
    private ISysUserService userService;
    @Autowired
    private IZcStoreInfoService storeInfoService;

    /**
     * 查询入库列表
     */
    @PreAuthorize("@ss.hasPermi('sales:storeEnter:list')")
    @GetMapping("/list")
    public TableDataInfo list(ZcStoreEnter zcStoreEnter)
    {
        startPage();
        List<ZcStoreEnter> list = zcStoreEnterService.selectZcStoreEnterList(zcStoreEnter);
        for (ZcStoreEnter storeEnter : list) {
            //入库人
            SysUser sysUser = userService.selectUserById(storeEnter.getStaffId());
            storeEnter.setStaffName(sysUser.getUserName());

        }
        return getDataTable(list);
    }

    /**
     * 导出入库列表
     */
    @PreAuthorize("@ss.hasPermi('sales:storeEnter:export')")
    @Log(title = "入库", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ZcStoreEnter zcStoreEnter)
    {
        List<ZcStoreEnter> list = zcStoreEnterService.selectZcStoreEnterList(zcStoreEnter);
        ExcelUtil<ZcStoreEnter> util = new ExcelUtil<ZcStoreEnter>(ZcStoreEnter.class);
        util.exportExcel(response, list, "入库数据");
    }

    /**
     * 获取入库详细信息
     */
    @PreAuthorize("@ss.hasPermi('sales:storeEnter:query')")
    @GetMapping(value = "/{enterId}")
    public AjaxResult getInfo(@PathVariable("enterId") Long enterId)
    {
        return success(zcStoreEnterService.selectZcStoreEnterByEnterId(enterId));
    }

    /**
     * 新增入库
     */
    @PreAuthorize("@ss.hasPermi('sales:storeEnter:add')")
    @Log(title = "入库", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ZcStoreEnter zcStoreEnter)
    {
        return toAjax(zcStoreEnterService.insertZcStoreEnter(zcStoreEnter));
    }

    /**
     * 修改入库
     */
    @PreAuthorize("@ss.hasPermi('sales:storeEnter:edit')")
    @Log(title = "入库", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ZcStoreEnter zcStoreEnter)
    {
        return toAjax(zcStoreEnterService.updateZcStoreEnter(zcStoreEnter));
    }

    /**
     * 删除入库
     */
    @PreAuthorize("@ss.hasPermi('sales:storeEnter:remove')")
    @Log(title = "入库", businessType = BusinessType.DELETE)
	@DeleteMapping("/{enterIds}")
    public AjaxResult remove(@PathVariable Long[] enterIds)
    {
        return toAjax(zcStoreEnterService.deleteZcStoreEnterByEnterIds(enterIds));
    }

    /**
     * 审核入库
     */
    @PreAuthorize("@ss.hasPermi('sales:storeEnter:check')")
    @Log(title = "入库", businessType = BusinessType.DELETE)
    @PutMapping("/check")
    public AjaxResult check(@RequestBody ZcStoreEnter zcStoreEnter){

        ZcStoreInfo zcStoreInfo = storeInfoService.selectZcStoreInfoByStoreId(zcStoreEnter.getStoreId());
        long storeCount = zcStoreInfo.getTrueCount() + zcStoreInfo.getUntrueCount();
        long produceCount = zcStoreInfo.getLine()*zcStoreInfo.getWide() - storeCount;
        long count = zcStoreInfo.getLine()*zcStoreInfo.getWide() - storeCount - Long.valueOf(zcStoreEnter.getMeters());

        if (count<0){
            return AjaxResult.error("当前仓储容量为"+produceCount+",仓储不足,请重新选择合适仓库");
        }
        if (count==0){
            zcStoreInfo.setStatus("1");
        }
        zcStoreInfo.setTrueCount(zcStoreInfo.getTrueCount()+Long.valueOf(zcStoreEnter.getMeters()));
        storeInfoService.updateZcStoreInfo(zcStoreInfo);
        return toAjax(zcStoreEnterService.checkZcStoreEnter(zcStoreEnter));
    }

    /**
     * 查询待审核数据
     */
    @GetMapping("/checkStatus")
    public AjaxResult checkStatus(){
        return success(zcStoreEnterService.checkAllStatus());
    }

    /**
     * 获取成品仓库列表
     */
    @PreAuthorize("@ss.hasPermi('sales:storeEnter:list')")
    @GetMapping("/okStoreList")
    public AjaxResult okStoreList(){
        List<ZcStoreInfo> okStore = storeInfoService.selectAllOkStore();
        return success(okStore);
    }



}
